﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Dispatcher;

namespace UFIDA.U9.WSS.ProxyService.Behavior.Endpoint
{
    /// <summary>
    ///     上下文扩展
    /// </summary>
    public class TokenEndpointBehaviorExtension : BehaviorExtensionElement
    {
        #region BehaviorExtensionElement

        public override Type BehaviorType
        {
            get { return typeof(TokenEndpointBehavior); }
        }

        protected override object CreateBehavior()
        {
            List<IDispatchMessageInspector> inspectors = new List<IDispatchMessageInspector>();
            foreach (BehaviorExtensionElement childBehavior in Behavior)
            {
                IDispatchMessageInspector inspector =
                    Activator.CreateInstance(childBehavior.BehaviorType) as IDispatchMessageInspector;
                if (inspector == null)
                    throw new Exception("behavior must inherit IDispatchMessageInspector");
                inspectors.Add(inspector);
            }

            return new TokenEndpointBehavior(inspectors);
        }

        [ConfigurationProperty("behavior", IsDefaultCollection = false)]
        public EndpointBehaviorElement Behavior
        {
            get
            {
                EndpointBehaviorElement behavior =
                    (EndpointBehaviorElement) base["behavior"];
                return behavior;
            }
        }

        #endregion
    }
}